/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-7
  File:    package.cpp  
  Description: 

*/

#include "stdafx.h"

using namespace m;

namespace m
{
	package::package()
		: _PackageName(nullptr),
		_Metadata(nullptr),
		_FirstFile(nullptr),
		_AssemblyDir(nullptr),
		_Result(0),
		_Overwrite(false)
	{
	}

	package::~package()
	{
		clear(_PackageName);
		clear(_Metadata);

		FileList* cur = _FirstFile;
		while (cur)
		{
			FileList* toDelete = cur;
			cur = cur->Next;
			delete toDelete;
		}

		clear(_AssemblyDir);
		clear(_Metadata);
	}

	object * package::metadata() 
	{
		return _Metadata;
	}

	void package::set_metadata(object * packageMetadata)
	{
		assign(_Metadata, packageMetadata);
	}
	
	void package::add_file(const wchar_t* archivePath, const wchar_t * filePath)
	{
		assert(archivePath && filePath);
		if (archivePath && filePath)
		{

			FileList* nextFile = new FileList();
			assign(nextFile->Root, archivePath);
			assign(nextFile->FilePath, filePath);
			nextFile->Next = _FirstFile;
			_FirstFile = nextFile;
		}
	}
	
	void package::add_file(const wchar_t * archivePath, io::file_buffer * buffer)
	{
		assert(archivePath && buffer);
		if (archivePath && buffer)
		{

			FileList* nextFile = new FileList();
			assign(nextFile->Root, archivePath);
			assign(nextFile->Buffer, buffer);
			nextFile->Next = _FirstFile;
			_FirstFile = nextFile;
		}

	}
	
	int m::package::get_result() const
	{
		return _Result;
	}
	
	package::FileList::FileList()
		: Root(nullptr),
		FilePath(nullptr),
		Buffer(nullptr),
		Next(nullptr)
	{

	}
	
	package::FileList::~FileList()
	{
		clear(Root);
		clear(FilePath);
		clear(Buffer);
	}
}